using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using BitMango.GDPR.Promotion;
using UnityEngine;

namespace BitMango
{
    public static class Platform
    {
        public static bool CheckAndFixedFirebase
        {
            get
            {
                return Platform.checkAndFixedFirebase;
            }
            set
            {
                Platform.checkAndFixedFirebase = value;
            }
        }

        public static bool FinishedCheckFirebase
        {
            get
            {
                return Platform.finishedCheckFirebase;
            }
            set
            {
                Platform.finishedCheckFirebase = value;
            }
        }

        public static PlatformContext Info
        {
            get
            {
                return Context.BMPlatform;
            }
        }

        public static GameObject Holder
        {
            get
            {
                return Module.Holder;
            }
        }

        public static bool IsEU()
        {
            return false;
        }

        public static bool IsTOSAgreed
        {
            get
            {
                if (PlayerPrefs.GetInt(Platform.CONSENT_DEPRECATED.GDPR_Agreed.ToString()) == 1 || PlayerPrefs.GetInt(Platform.CONSENT_DEPRECATED.BM_TermsAgree.ToString()) == 1)
                {
                    PlayerPrefs.SetString(Platform.CONSENT.TOS_Agreed.ToString(), "yes");
                    PlayerPrefs.SetInt(Platform.CONSENT_DEPRECATED.GDPR_Agreed.ToString(), 1);
                    PlayerPrefs.SetInt(Platform.CONSENT_DEPRECATED.BM_TermsAgree.ToString(), 1);
                    PlayerPrefs.Save();
                }
                return Platform.GetConsentAgree(Platform.CONSENT.TOS_Agreed);
            }
            set
            {
                Platform.SetConsentAgree(Platform.CONSENT.TOS_Agreed, value);
            }
        }

        public static bool IsTargetingAdsAgreed
        {
            get
            {
                return Platform.GetConsentAgree(Platform.CONSENT.Targeting_Ads_Agreed);
            }
            set
            {
                Platform.SetConsentAgree(Platform.CONSENT.Targeting_Ads_Agreed, value);
            }
        }

        private static bool GetConsentAgree(Platform.CONSENT type)
        {
            return PlayerPrefs.GetString(type.ToString()) == "yes";
        }

        private static void SetConsentAgree(Platform.CONSENT type, bool isAgree)
        {
            Platform.PlatformModulesGDPRStateChange(isAgree);
            PlayerPrefs.SetString(type.ToString(), (!isAgree) ? "no" : "yes");
            PlayerPrefs.Save();
            DWATTLight.logEvent(type.ToString(), new object[]
            {
                (!isAgree) ? 0 : 1
            });
            GDPRAgreeAdvertisement.ConsentAgree(isAgree);
        }

        private static void PlatformModulesGDPRStateChange(bool isAgreed)
        {
            UnityEngine.Debug.Log("Platform::PlatformModulesGDPRStateChange");
            if (isAgreed)
            {
            }
        }

        public static void Init()
        {
            Module.Init();
            ControlCenter.GetControlCenterInfo();
            Profile.Init();
        }

        public static void SystemAlert(string title, string message, string positiveButton, string negativeButton = null, Action<bool> callback = null)
        {
        }

        public static void SafeCallback(Action func)
        {
            if (func != null)
            {
                func();
            }
        }

        public static void SafeCallback<T>(Action<T> func, T param)
        {
            if (func != null)
            {
                func(param);
            }
        }

        public static void SafeCallback<T1, T2>(Action<T1, T2> func, T1 param1, T2 param2)
        {
            if (func != null)
            {
                func(param1, param2);
            }
        }

        public static void BroadcastMessage(string msg, object param = null)
        {
            foreach (Transform transform in UnityEngine.Object.FindObjectsOfType<Transform>())
            {
                if (transform.parent == null)
                {
                    transform.BroadcastMessage(msg, param, SendMessageOptions.DontRequireReceiver);
                }
            }
        }

        public static void SetRecoverPoint(string name)
        {
            Context.BMPlatform.recoverPoint = name;
        }

        private static bool checkAndFixedFirebase;

        private static bool finishedCheckFirebase;


        public enum CONSENT
        {
            TOS_Agreed,
            Targeting_Ads_Agreed
        }

        public enum CONSENT_DEPRECATED
        {
            GDPR_Agreed,
            BM_TermsAgree
        }
    }
}
